package com.sh.weblogspringboot.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sh.weblogspringboot.admin.convert.BlogSettingsConvert;
import com.sh.weblogspringboot.admin.service.AdminBlogSettingsService;
import com.sh.weblogspringboot.admin.vo.blogsettings.FindBlogSettingsRspVO;
import com.sh.weblogspringboot.admin.vo.blogsettings.UpdateBlogSettingsReqVO;
import com.sh.weblogspringboot.common.domain.dos.BlogSettingsDO;
import com.sh.weblogspringboot.common.domain.mapper.BlogSettingsMapper;
import com.sh.weblogspringboot.common.util.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author songHai
 * @Classname AdminBlogSettingsServiceImpl
 * @Description 博客设置信息 Service实现类
 * @Date 2025/4/7 19:22
 */
@Service
@Slf4j
public class AdminBlogSettingsServiceImpl extends ServiceImpl<BlogSettingsMapper, BlogSettingsDO> implements AdminBlogSettingsService {

    @Autowired
    private BlogSettingsMapper blogSettingsMapper;

    @Override
    public Response updateBlogSettings(UpdateBlogSettingsReqVO updateBlogSettingsReqVO) {

        // VO 转 DO
        BlogSettingsDO blogSettingsDO = BlogSettingsConvert.INSTANCE.convertVO2DO(updateBlogSettingsReqVO);

        // 个人博客信息设置，所有一条记录，ID固定值为1L
        // ID为1，如果记录存在则更新，否则执行保存
        blogSettingsDO.setId(1L);

        // 保存或者更新
        boolean isSuccess = saveOrUpdate(blogSettingsDO);

        return isSuccess ? Response.success("更新成功") : Response.fail("更新失败");
    }

    @Override
    public Response findDetail() {
        // 查询ID为1的记录
        BlogSettingsDO blogSettingsDO = blogSettingsMapper.selectById(1L);

        // DO转VO
        FindBlogSettingsRspVO vo = BlogSettingsConvert.INSTANCE.convertDO2VO(blogSettingsDO);

        if (Objects.nonNull(blogSettingsDO)) {
            return Response.success("查询成功", vo);
        } else {
            return Response.fail("查询失败");
        }
    }
}
